const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();

// 自定义中间件
server.use(jsonServer.bodyParser); // 解析请求体，如果是POST/PUT/DELETE等请求
server.use((req, res, next) => {
    if (req.url.startsWith('/goods/')) {
        const goodsId = req.url.substring('/goods/'.length);
        // 将动态部分转换为查询字符串，以便路由器识别
        req.url = '/goods';
        req.query.id = goodsId;
    }
    next();
});

server.use(middlewares);
server.use(router);
server.listen(3000, () => {
    console.log('JSON Server is running');
});